package org.gjy.m8.tool;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-09-07 14:50:13
 */
public class Work3 implements IWorker<String, String>, ICallback<String, String> {
    private static final Logger log = LoggerFactory.getLogger(Work3.class);
    private final String CLASS_NAME = Work3.class.getSimpleName();

    @Override
    public void begin() {
        log.info("{} begin", CLASS_NAME);
    }

    @Override
    public void result(boolean success, String param, WorkResult<String> workResult) {
        if (success) { // 执行成功
            log.info("{} success={}, param={}, result={}", CLASS_NAME, true, param, workResult);
        } else {
            log.error("{} success={}, param={}, result={}", CLASS_NAME, false, param, workResult);
        }
    }

    @Override
    public String action(String param, Map<String, WorkerWrapper> allWrappers) {
        log.info("{} param={},wrappers={}", CLASS_NAME, param, allWrappers);
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return CLASS_NAME + " action";
    }

    @Override
    public String defaultValue() {
        log.info("{} defaultValue", CLASS_NAME);
        return CLASS_NAME;
    }
}
